<?php

namespace App\Http\Controllers\Api\v1;

use App\Http\Controllers\Controller;
use App\Services\Easywechat\PayService;
use App\Utils\AppResult;
use App\Utils\Common;
use App\Utils\Control\AuthUser;

/**
 * 支付示例
 * Author:panda
 * Email:joeyoung0314@qq.com
 * Class PaymentController
 * @package App\Http\Controllers\Api\v1
 */
class PaymentController extends Controller
{
    // 日志模块名称
    const LOG_MODULE = 'v1/PaymentController';

    /**
     * 微信支付示例 v3
     * @return string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function wxMiniPayV3()
    {
        $user = AuthUser::getInstance()->get();

        $pay = new PayService();
        $res = $pay->jsapi(
            openid      : $user['openid'],
            out_trade_no: date('YmdHis'),
            fee         : 1,
            description : '商品购买',
            notify_url  : Common::getHttp('/notify/wx/dealWith')
        );
        if (!$res) {
            return AppResult::response201($pay->getMessage() ?? '下单失败');
        }

        return AppResult::response200(result: $pay->get());
    }

    /**
     * App微信支付示例 v3
     * TODO 有待测试
     *
     * @return string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function wxAppPayV3()
    {
        $pay = new PayService();
        $res = $pay->appPay(
            out_trade_no: date('YmdHis'),
            fee         : 1,
            description : '商品购买',
            notify_url  : Common::getHttp('/notify/wx/dealWith')
        );
        if (!$res) {
            return AppResult::response201($pay->getMessage() ?? '下单失败');
        }

        return AppResult::response200(result: $pay->get());
    }

    /**
     * H5微信支付示例 v3
     * TODO 有待测试
     * @return string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function wxH5PayV3()
    {
        $pay = new PayService();
        $res = $pay->h5Pay(
            out_trade_no: date('YmdHis'),
            fee         : 1,
            description : '商品购买',
            notify_url  : Common::getHttp('/notify/wx/dealWith')
        );
        if (!$res) {
            return AppResult::response201($pay->getMessage() ?? '下单失败');
        }

        return AppResult::response200(result: $pay->get());
    }

}
